﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services.DataSource.Schedule
{
    public class DB_Schedules
    {
        public List<Schedules> GetMySchedulesByStatus(string uid, int status)
        {
            string sql = "select * from Schedules where status=@status and senduid=@senduid";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@status",status),
                new SqlParameter("@senduid",uid)
            };
            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
            }
            catch (Exception)
            {
                throw;
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ConvertListAndTable.DataTableToList<Schedules>(dt);
            }
        }

        public List<Schedules> GetMyApproveSchedules(string uid, int status)
        {
            string sql = "select * from Schedules where approveuid = @approveuid and status =@status";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@approveuid",uid),
                new SqlParameter("@status",status)
            };
            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
            }
            catch (Exception)
            {
                throw;
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ConvertListAndTable.DataTableToList<Schedules>(dt);
            }
        }

        public bool Insert(Schedules schedule)
        {
            string sql = "insert into Schedules values(@Title,@Contents,@BeginDate,@ApproveDate,@SendUid,@ApproveUid,@ApproveContent,@Status)";

            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@Title",schedule.Title),
                new SqlParameter("@Contents",schedule.Contents),
                new SqlParameter("@BeginDate",schedule.BeginDate),
                new SqlParameter("@ApproveDate",schedule.ApproveDate),
                new SqlParameter("@SendUid",schedule.SendUid),
                new SqlParameter("@ApproveUid",schedule.ApproveUid),
                new SqlParameter("@ApproveContent",schedule.ApproveContent),
                new SqlParameter("@Status",schedule.Status)
            };

            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public bool Update(Schedules schedule)
        {
            string sql = "update Schedules set Title= @Title, Contents = @Contents,BeginDate = @BeginDate,ApproveDate=@ApproveDate,SendUid=@SendUid,ApproveUid=@ApproveUid,ApproveContent=@ApproveContent,Status=@Status where id=@id";

            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@Title",schedule.Title),
                new SqlParameter("@Contents",schedule.Contents),
                new SqlParameter("@BeginDate",schedule.BeginDate),
                new SqlParameter("@ApproveDate",schedule.ApproveDate),
                new SqlParameter("@SendUid",schedule.SendUid),
                new SqlParameter("@ApproveUid",schedule.ApproveUid),
                new SqlParameter("@ApproveContent",schedule.ApproveContent),
                new SqlParameter("@Status",schedule.Status),
                new SqlParameter("@id",schedule.Id),
            };

            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public bool Delete(int scheduleId)
        {
            string sql = "delete Schedules where id=@id";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@id",scheduleId),
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public Schedules GetScheduleById(string id)
        {
            string sql = "select * from Schedules where id=@id";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@id",id)
            };
            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
            }
            catch (Exception)
            {
                throw;
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                Schedules s = new Schedules();
                s.Id = int.Parse(dt.Rows[0]["Id"].ToString());
                s.Title = dt.Rows[0]["Title"].ToString();
                s.Contents = dt.Rows[0]["Contents"].ToString();
                s.BeginDate = dt.Rows[0]["BeginDate"].ToString();
                s.ApproveDate = dt.Rows[0]["ApproveDate"].ToString();
                s.SendUid = dt.Rows[0]["SendUid"].ToString();
                s.ApproveUid = dt.Rows[0]["ApproveUid"].ToString();
                s.ApproveContent = dt.Rows[0]["ApproveContent"].ToString();
                s.Status = int.Parse(dt.Rows[0]["Status"].ToString());

                return s;
            }
        }
    }
}
